<?php
namespace  app\service;
use app\exception\ThirdApiException;
use GuzzleHttp\Client;
use plugin\saiadmin\app\logic\system\SystemConfigLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Arr;
use Illuminate\Support\Arr as CommonArr;

class PushService
{

    public function push($requestData)
    {
        //获取设置的推送配置信息
        $logic = new SystemConfigLogic();
        $uploadConfig = $logic->getGroup('push_config');
        $webhookUrl = Arr::getConfigValue($uploadConfig, 'webhook_url');
        if(!$webhookUrl){
            throw new ApiException('你还未设置推送配置');
        }
        $msgType = CommonArr::get($requestData,'msg_type','');
        $payload = [
            'msgtype' => $msgType,
        ];
        switch ($msgType) {
            default:
            case "text":
               $payload['text'] = ['content' => CommonArr::get($requestData,'content'),'mentioned_list' => ['@all']];
                break;
            case "markdown":
                $payload['markdown'] = ['content' => CommonArr::get($requestData,'markdown_content')];
                break;
            case "image":
                if(env('APP_ENV') == 'development'){
                    $image = 'http://39.98.91.89:8787/storage/20240626/02b944af62020b94bfd7d68d1b2a1d25f6f919c1.jpg';
                }else{
                    $image = $requestData['image'];
                }
                $opts = [
                    'http' => [
                        'method'  => "GET",
                        'timeout' => 3,  // 设置超时时间为3秒
                    ]
                ];
                // 创建流上下文资源 stream_context_create
                $imageContent = file_get_contents($image, false, stream_context_create($opts));
                if(!$imageContent){
                    throw new ApiException('获取图片信息失败');
                }
                $payload['image'] = ['base64' =>base64_encode($imageContent),'md5'=>md5($imageContent)];
                break;
            case "news":
                $payload['news'] = [
                    'articles' => [
                        "title" => CommonArr::get($requestData,'title'),
                        "description" => CommonArr::get($requestData,'description',''),
                        "url" => CommonArr::get($requestData,'url'),
                        "picurl" => CommonArr::get($requestData,'picurl','')
                    ]
                ];
                break;
        }
       return $this->pushWxWorkMsg($webhookUrl,$payload);
    }

    //推送机器人企业信息
    public function pushWxWorkMsg($webhookUrl, $data)
    {
        try {
            $client = new Client();
            $response = $client->post($webhookUrl, ['verify' => false, 'timeout' => 5, 'json' => $data]);
            $pushResult = json_decode($response->getBody()->getContents(), true);
        } catch (\Exception $e) {
            throw new ApiException('推送消息失败');
        }
        if($pushResult['errcode'] != 0){
            throw new ApiException('推送消息失败'.$pushResult['errmsg']);
        }
        return $pushResult;
    }
}
